﻿using B.S.DataBase.MyProject.Domain.Management;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.Management;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.Management
{
    /// <summary>
    /// 处理删除健康报告命令
    /// </summary>
    public class DelReportsCommandHandler : IRequestHandler<DelReportsCommand, APIResult<object>>
    {
        /// <summary>
        /// sqlSugar
        /// </summary>
        private readonly SqlSugarContext sqlSugar;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<DelegatingHandler> logger;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="sqlSugar">sqlSugar</param>
        /// <param name="logger">日志</param>
        public DelReportsCommandHandler(SqlSugarContext sqlSugar,ILogger<DelegatingHandler> logger)
        {
            this.sqlSugar = sqlSugar;
            this.logger = logger;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<object>> Handle(DelReportsCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<object> result=new APIResult<object>();
                var entity=sqlSugar.db.Deleteable<Reports>().Where(x=>x.ReportsId==request.ReportsId).ExecuteCommand();
                if (entity >0)
                {
                    result.Code = APIEnum.删除成功;
                    result.Msg = APIEnum.删除成功.ToString();
                    result.data = entity;
                }
                else
                {
                    result.Code = APIEnum.删除失败;
                    result.Msg = APIEnum.删除失败.ToString();
                    result.data = entity;
                }
                return await Task.FromResult(result);
            }
            catch (Exception ex)
            {
                logger.LogError("删除失败"+ex.Message,ex);
                throw;
            }
        }
    }
}
